﻿#region Legal

// Jimmy Zimmerman
// Team Mongoose
//
// END USER LICENSE AGREEMENT
// IMPORTANT - READ THIS CAREFULLY:  This End User License Agreement is a legal agreement
// between you (either an individual, business entity, currently active identity of an
// individual with Multiple Personality Disorder, octopus overlord, or otherworldly entity),
// and Team Mongoose, for the enclosed, components.
//
// By reading this document and/or installing this product, you agree to be bound by the
// terms of this EULA.
//
// Team Mongoose owns all copyright, trade secret, trademark, trade wars,
// patent, portent, and potential rights to these components.  Team Mongoose
// grants you the right to deploy the enclosed components.
//
// If you agree to the terms of this EULA, a license to use these components is granted to you.
//
// If you should happen to benefit from the enclosed components, then you are legally and
// contractually bound to thank us for it. Send your regards to jimmyzimms@hotmail.com.
//
// OTHER RIGHTS AND LIMITATONS
// You may not reverse-engineer, decompile, decode, disassemble, psycho-analyze, or otherwise
// attempt to find hidden meanings between the lines of this EULA; unless, of course, you should
// happen to find some, and they are funny.
//
// You may not translate this EULA or any part of the components into Ancient Sumerian.
//
// THESE COMPONENTS ARE PROVIDED “AS-IS” WITHOUT WARRANTY OF ANY KIND. ANY USE OF THE COMPONENTS
// CONTAINED IS AT YOUR OWN RISK. TEAM MONGOOSE DISCLAIM ALL WARRANTIES, EITHER
// EXPRESS OR IMPLIED, WITH RESPECT TO THE ACCURRACY AND CORRECTNESS OF THE COMPONENTS CONTAINED
// HEREIN. TEAM MONGOOSE DOES NOT WARRANT THAT THE COMPONENTS ARE FLAWLESS.
//
// REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS, WITH OR WITHOUT MODIFICATION, ARE PERMITTED
// PROVIDED THAT THE FOLLOWING CONDITIONS ARE MET:
// * REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THE ABOVE COPYRIGHT NOTICE
// * REDISTRIBUTIONS IN BINARY FORM MUST NOTE THE USE OF THE COMPONENT IN DOCUMENTATION AND/OR
//   OTHER MATERIALS PROVIDED WITH THE DISTRIBUTION.
// * NEITHER THE NAME OF TEAM MONGOOSE MAY BE USED TO ENDORES OR PROMOTE PRODUCTS
//   DERIVED FROM THIS SOFTWARE WITHOUT SPECIFIC PRIOR WRITTEN PERMISSION.
//
// IN NO EVENT SHALL TEAM MONGOOSE BE HELD LIABLE FOR INCIDENTAL, SPECIAL, INDIRECT,
// INCONSEQUENTIAL, UNBELIEVABLE, EXAGGERATED, VERBOSE, OR TYPICAL DAMAGES INCURRED WHILE USING
// THE ENCLOSED COMPONENTS.
//
// OUR STUFF ALWAYS WORKS - SOMETIMES.

#endregion

using System;

namespace Mongoose.Games.BfgRoster
{
    /// <summary>
    /// Represents an ordinance weapon in the BFG game.
    /// </summary>
    public class Ordinance : IMutableLockingItem
    {
        #region Fields

        private Boolean isLocked;
        private String name;
        private Int32 speed;
        private Int32 deploymentsCapable;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="Ordinance"/> class.
        /// </summary>
        /// <param name="name">The name or title of the ordinance weapon.</param>
        /// <param name="speed">The speed of the ordinance weapon.</param>
        /// <param name="deploymentsCapable">The number of deployments available before an ordinance weapon requires a reload.</param>
        /// <exception cref="ArgumentNullException">The supplied value is null, empty, or whitespace.</exception>
        public Ordinance(String name, Int32 speed, Int32 deploymentsCapable)
        {
            name = (name ?? String.Empty).Trim();
            if (name.IsNullOrWhiteSpace()) throw new ArgumentNullException("name", "The value cannot be null, empty, or only contain whitespace characters.");
            if (speed < 1) throw new ArgumentOutOfRangeException("speed", String.Format(null, "The value must be at least 1. Actual value '{0}'", speed));
            if (deploymentsCapable < 1) throw new ArgumentOutOfRangeException("deploymentsCapable", String.Format(null, "The value must be at least 1. Actual value '{0}'", deploymentsCapable));

            this.name = name;
            this.speed = speed;
            this.deploymentsCapable = deploymentsCapable;
        }

        #endregion
        
        #region Properties

        /// <summary>
        /// Gets or sets the name or title of the ordinance weapon.
        /// </summary>
        /// <value>The name or title of the ordinance weapon.</value>
        /// <exception cref="InvalidOperationException">The current instance is locked for modification.</exception>
        /// <exception cref="ArgumentNullException">The supplied value is null, empty, or whitespace.</exception>
        public virtual String Name
        {
            get { return this.name; }
            set
            {
                value = (value ?? String.Empty).Trim();
                if (value.IsNullOrWhiteSpace()) throw new ArgumentNullException("Name", "The value cannot be null, empty, or only contain whitespace characters.");

                this.CheckLock();

                this.name = value;
            }
        }

        /// <summary>
        /// Gets or sets the base speed of the ordinance weapon.
        /// </summary>
        /// <value>The base speed of the ordinance weapon.</value>
        /// <exception cref="InvalidOperationException">The current instance is locked for modification.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The supplied value is less than 1.</exception>
        public virtual Int32 Speed
        {
            get { return this.speed; }
            set
            {
                if (value < 1) throw new ArgumentOutOfRangeException("Speed", String.Format(null, "The value must be at least 1. Actual value '{0}'", value));
                this.CheckLock();

                this.speed = value;
            }
        }

        /// <summary>
        /// Gets or sets the number of deployments the ordinance weapon is capable of per reload.
        /// </summary>
        /// <value>The number of deployments the ordinance weapon is capable of per reload.</value>
        /// <exception cref="InvalidOperationException">The current instance is locked for modification.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The supplied value is less than 1.</exception>
        public virtual Int32 DeploymentsCapable
        {
            get { return this.deploymentsCapable; }
            set
            {
                if (value < 1) throw new ArgumentOutOfRangeException("DeploymentsCapable", String.Format(null, "The value must be at least 1. Actual value '{0}'", value));
                this.CheckLock();

                this.deploymentsCapable = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Used to confirm that the current ordinance weapon instance is not <see cref="IsLocked">locked</see> for modification.
        /// </summary>
        /// <remarks>Properties that can only be modified until the instance is <see cref="Lock">locked</see> should call this method before changing the stored value.</remarks>
        /// <see cref="InvalidOperationException">The current ordinance weapon instance is locked for modification.</see>
        protected virtual void CheckLock()
        {
            if (this.IsLocked) throw new InvalidOperationException("The current ordinance instance is locked for modification of the weapon characteristics.");
        }
        
        #endregion

        #region IMutableLockingItem Members

        /// <summary>
        /// Indicates if the current instance is locked for further modification.
        /// </summary>
        /// <value>True if the current instance is locked for further modification; otherwise false.</value>
        public virtual Boolean IsLocked
        {
            get { return this.isLocked; }
        }

        /// <summary>
        /// Locks the current instance settings against further modification.
        /// </summary>
        /// <remarks>
        /// Multiple calls to this method will not raise an exception. Once locked, this instance will remain immutable
        /// for the remainder of the game.
        /// </remarks>
        public virtual void Lock()
        {
            this.isLocked = true;
        }

        #endregion
    }
}
